package com.powernode.oss.util;

import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import com.powernode.dto.Result;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.Json;
import io.minio.MinioClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
* 文件上传和删除工具类
*/
public class QiniuUtil {
    private static final String ACCESS_KEY = "3W1AhUF7fcOrqHvlr0wgOft6cuwnSw8WaaacLwoz";
    private static final String SECRET_KEY = "RX1ef7qUsul-2wV9EZuk4g31c0TAoTfFGtHfLy5X";
    private static final String BUCKET = "pn-mall33";
    private static final String DOMAIN = "http://roelrepdw.hn-bkt.clouddn.com/";

    /**
     * @param file:
	 * @param dir:
     * @return Result
     * @description TODO 文件上传
     * @author Juice
     * @date 2023/1/13 15:27
     **/
    public static  Result upload(MultipartFile file,String dir){

        //设置文件在云空间的存储名
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM/dd");
        String now = sdf.format(new Date());
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String filename =  dir +"/"+ now + "/" + UUID.randomUUID() + fileType;
        //
        Configuration conf = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(conf);
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        String token = auth.uploadToken(BUCKET);
        try {
            Response response = uploadManager.put(file.getInputStream(), filename, token,null,null);
            DefaultPutRet putRet = JSONUtil.toBean(response.bodyString(), DefaultPutRet.class);
            Map<String,Object> data = new HashMap<>();
            data.put("name",filename);
            data.put("url",DOMAIN+filename);
            return Result.success("文件上传成功",data);
        } catch (QiniuException e) {
            String msg = e.response.toString();
            try {
                msg = e.response.bodyString();
            } catch (QiniuException ex) {
                ex.printStackTrace();
            }
            System.err.println(msg);
            return Result.failure(HttpStatus.HTTP_INTERNAL_ERROR, msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
    * 文件删除
    */
    public static Result deleteFile(String filename){
        Configuration conf = new Configuration(Zone.zone2());
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        BucketManager bucketManager = new BucketManager(auth, conf);
        try {
            bucketManager.delete(BUCKET,filename);
            return Result.success("删除成功",null);
        } catch (QiniuException e) {
            System.err.println(e.response.toString());
            return Result.failure(HttpStatus.HTTP_INTERNAL_ERROR,e.response.toString());
        }
    }
}
